package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.CategoryMapper;
import cn.usian.dao.DishMapper;
import cn.usian.dao.SetmealMapper;
import cn.usian.entity.Category;
import cn.usian.entity.Dish;
import cn.usian.entity.Setmeal;
import cn.usian.exception.CustomerException;
import cn.usian.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    @Override
    public R<Page> page(int page, int pageSize) {
        Page<Category> objectPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        List<Category> categories = categoryMapper.selectList(null);
        for (int i = 0; i < categories.size(); i++) {
            queryWrapper.orderByAsc(Category::getSort);
        }
        Page<Category> categoryPage = categoryMapper.selectPage(objectPage, queryWrapper);
        return R.success(categoryPage);
    }

    @Override
    public void save(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.insert(category);
    }

    @Override
    public void del(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, id);
        List<Dish> dish = dishMapper.selectList(queryWrapper);
        if (!dish.isEmpty()) {
            throw new CustomerException("该分类已关联其他菜品不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper01 = new LambdaQueryWrapper<>();
        queryWrapper01.eq(Setmeal::getCategoryId, id);
        List<Setmeal> setmeal = setmealMapper.selectList(queryWrapper01);
        if (!setmeal.isEmpty()) {
            throw new CustomerException("该分类已关联其他套餐不能删除");
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public void updatecategory(Category category) {
        LambdaUpdateWrapper<Category> queryWrapper = new LambdaUpdateWrapper<>();
        category.setUpdateTime(LocalDateTime.now());
        if (category.getName() != null && category.equals("")) {
            queryWrapper.set(Category::getName,category.getName());
        }
        if (category.getType() != null && category.equals("")) {
            queryWrapper.set(Category::getType,category.getType());
        }
        if (category.getSort() != null && category.equals("")) {
            queryWrapper.set(Category::getSort,category.getSort());
        }
        categoryMapper.updateById(category);
    }

    @Override
    public List<Category> findAll() {
        return categoryMapper.selectList(null);
    }

    @Override
    public R<List<Category>> findByList(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        List<Category> list = categoryMapper.selectList(queryWrapper);
        return R.success(list);
    }


}
